import org.apache.commons.io.FileUtils;
import org.apache.lucene.document.*;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.File;

public class IndexCreator {

    @Test
    public void createIndexBase()throws  Exception{

        //1.Create directory which stores index
//        Directory directory = new RAMDirectory();//Keep data in ram
        Directory directory = FSDirectory.open(new File("..\\Documents\\index").toPath());
        //2.Create an Object of IndexWrite based on Obect Directory
        IndexWriterConfig config = new IndexWriterConfig((new IKAnalyzer()));
//        IndexWriter indexWriter = new IndexWriter(directory,new IndexWriterConfig());
        IndexWriter indexWriter = new IndexWriter(directory,config);

        //3.Read files in disk and create file objects for each of them
        File dir_url = new File("..\\Documents\\url");
        File[] files_url = dir_url.listFiles();
        for(File f:files_url){
            //Get ID
            String ID = f.getName();
            //Ger Url
            String url = FileUtils.readFileToString(f,"utf-8");
            //Get title
            File fileTitle = new File("..\\Documents\\title\\"+ID);
            String title = FileUtils.readFileToString(fileTitle,"utf-8");
            //Get content
            File fileContent = new File("..\\Documents\\content\\"+ID);
            String content = FileUtils.readFileToString(fileContent,"utf-8");
            //Create field
            Field fieldTitle = new TextField("name",title,Field.Store.YES);
            Field fieldUrl = new StoredField("url",url);
            Field fieldContent = new TextField("content",content,Field.Store.YES);

            //Create document object
            Document document = new Document();
            //Add field into it
            document.add(fieldTitle);
            document.add(fieldUrl);
            document.add(fieldContent);
            //Write documents into  Library
            indexWriter.addDocument(document);
        }
        indexWriter.close();
    }
}
